package com.util;


import com.entity.dto.ParameterBeanVo;
import org.springframework.stereotype.Component;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**

 **/
@Component
public class PublicMethods {


    public String getTableFromFlag(String flag) {
        switch (flag) {
            case "1min_tjl":
                return "NEW_DW_1MIN_TJL";
            case "5min_tjl":
                return "NEW_DW_5MIN_TJL";
            case "15min_tjl":
                return "NEW_DW_15MIN_TJL";
            case "15min_lj":
                return "NEW_DW_15MIN_LJ_2";
            case "1hour_lj":
                return "NEW_DW_1H_LJ_2";
            case "day_tjl":
                return "NEW_DW_DAY_TJL";
            case "day_lj":
                return "NEW_DW_DAY_TJL_LJ";
            case "month_tjl":
                return "NEW_DW_MONTH_TJL";
            case "month_lj":
                return "NEW_DW_MONTH_TJL_LJ";
            case "year_tjl":
                return "NEW_DW_YEAR_TJL";
            default:
                return "NEW_DW_YEAR_TJL_LJ";
        }
    }

    public String getHiveTableFromFlag(String flag) {
        switch (flag) {
            case "1min_tjl":
                return "app_zhny_ycl_tjl_15min";
            case "5min_tjl":
                return "app_zhny_ycl_tjl_15min";
            case "15min_tjl":
                return "app_zhny_ycl_tjl_15min";
            case "15min_lj":
                return "app_zhny_ycl_lj_15min";
            case "day_tjl":
                return "dw_day_tjl";
            case "day_lj":
                return "dw_day_tjl_lj";
            case "month_tjl":
                return "NEW_DW_MONTH_TJL";
            case "month_tjl_lj":
                return "NEW_DW_MONTH_TJL_LJ";
            case "year_tjl":
                return "NEW_DW_YEAR_TJL";
            default:
                return "NEW_DW_YEAR_TJL_LJ";
        }
    }

    public String getTableFromFlagMeiShan(String flag) {
        switch (flag) {
            case "1min_tjl":
                return "NEW_DW_1MIN_TJL";
            case "15min_tjl":
                return "NEW_DW_15MIN_TJL_36";
            case "15min_lj":
                return "NEW_DW_15MIN_LJ_36";
            case "30min_tjl":
                return "NEW_DW_30MIN_TJL_36";
            case "30min_lj":
                return "NEW_DW_30MIN_LJ_36";
            case "1hour_tjl":
                return "NEW_DW_1H_TJL_36";
            case "1hour_lj":
                return "NEW_DW_1H_LJ_36";
            case "day_tjl":
                return "NEW_DW_DAY_TJL";
            case "day_lj":
                return "NEW_DW_DAY_TJL_LJ";
            case "month_tjl":
                return "NEW_DW_MONTH_TJL";
            case "month_lj":
                return "NEW_DW_MONTH_TJL_LJ";
            case "year_tjl":
                return "NEW_DW_YEAR_TJL";
            default:
                return "NEW_DW_YEAR_TJL_LJ";
        }
    }


    /**
     * 将传入的List<String>转成String
     *
     * @param : List<String> list 包含站点Id的集合
     * @return : String 转化的特定格式的字符串
     */
    public static String listToString(List<String> list) {
        StringBuffer sb = new StringBuffer();
        if (list.size() > 0) {
            for (String s : list) {
                if (StringUtil.isNotEmpty(s)) {
                    sb.append("'" + s + "',");
                } else {
                    continue;
                }
            }
            return sb.toString().substring(0, sb.length() - 1);
        } else {
            System.out.println("传入的list集合为空！");
            return null;
        }
    }




    //时间比较
    public static boolean compareTime(String time) {
        Boolean flag = false;
        try {
            if (StringUtil.isEmpty(time) || "".equalsIgnoreCase(time)) {
                return flag;
            }
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date nowTime = new Date();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(nowTime);
            calendar.add(Calendar.DATE, -90);
            Date baseTime = calendar.getTime();
            Date compareTime = format.parse(time);
            //前者大于后者 返回大于0的数字反之小于0的数字，等于返回0
            switch (compareTime.compareTo(baseTime)) {
                case 1:
                    flag = false;
                    break;
                case -1:
                    flag = true;
                    break;
                case 0:
                    flag = false;
                    break;
                default:
                    flag = false;

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    //选择phoenix还是hive
    public static boolean getPhoenixOrHiveTableFlag(String tableFlag) {
        switch (tableFlag) {
            case "15min_tjl":
                return true;
            case "15min_lj":
                return true;
            case "day_tjl":
                return false;
            case "day_lj":
                return false;
            case "month_tjl":
                return false;
            case "year_tjl":
                return false;
            default:
                return false;
        }
    }

    //时间比较
    public static boolean compareTime(List<String> time) {
        Boolean flag = false;
        try {
            if (time == null || time.size() == 0) {
                return flag;
            }
            for (int i = 0; i < time.size(); i++) {
                flag = compareTime(time.get(i));
                if (flag) {
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 实时量转成累计量
     *
     * @param dataFormart 需要将实时数据转换成累计数据，所传入的数据，第一个map的key为measuretag，第二个map的key为measurepointid，第三个map的key为sampletime
     * @param startTime   开始时间
     * @param endTime     结束时间
     */
    public Map<String, Map<String, Map<String, Double>>> query15MinLj(Map<String, Map<String, Map<String, Object>>> dataFormart, String startTime, String endTime) {
        Map<String, Map<String, Map<String, Double>>> resultMap = new LinkedHashMap<>();
        try {
            //stream根据tag多线程计算
            dataFormart.entrySet().stream()
                    .forEach(measure -> {
                        try {
                            resultMap.put(measure.getKey(), measurePointMap(measure.getValue(), startTime, endTime));
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * double转String
     *
     * @return
     */
    public static Object doubleToString(Object value) {
        if (value == null) {
            return value;
        }
        DecimalFormat df = new DecimalFormat("#.##");
        String number = df.format(value);
        return number;
    }

    /**
     * 实时量转成累计量
     *
     * @param dataFormart 需要将实时数据转换成累计数据，所传入的数据，第1个map的key为measurepointid，第2个map的key为sampletime
     * @param startTime   开始时间
     * @param endTime     结束时间
     */
    public static Map<String, Map<String, Double>> measurePointMap(Map<String, Map<String, Object>> dataFormart, String startTime, String endTime) throws ParseException {
        Long mils = System.currentTimeMillis();
        //开始毫秒值
        Long startDate = 0L;
        //结束毫秒值
        Long endDate = 0L;
        //差值
        int size = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        startDate = sdf.parse(startTime).getTime();
        endDate = sdf.parse(endTime).getTime();
        //有多少个15分钟
        size = (int) (endDate - startDate) / (1000 * 60 * 15);
        Date date = new Date();
        Date endDate1 = new Date();
        Map<String, Map<String, Double>> measurepointMap = new HashMap<>();
        for (String measurepointId : dataFormart.keySet()) {
            Map<String, Double> map = new LinkedHashMap<>();
            Map<String, Object> chars = dataFormart.get(measurepointId);
            for (int i = 0; i < size; i++) {
                date.setTime(startDate + (i * (1000 * 60 * 15)));
                endDate1.setTime(startDate + ((i + 1) * (1000 * 60 * 15)));
                String start = sdf.format(date);
                String end = sdf.format(endDate1);
                //数据缺失或数据为0不计入计算
                if (chars.get(end) != null && chars.get(start) != null && Double.valueOf((String) doubleToString(chars.get(start))) > 0 && Double.valueOf((String) doubleToString(chars.get(end))) > 0 && Double.valueOf((String) doubleToString(chars.get(end))) >= Double.valueOf((String) doubleToString(chars.get(start)))) {
                    map.put(start, Double.valueOf((String) doubleToString(Double.valueOf((String) doubleToString(chars.get(end))) - Double.valueOf((String) doubleToString(chars.get(start))))));
                }
            }
            measurepointMap.put(measurepointId, map);

        }
        return measurepointMap;
    }


}
